import sys

import pyglet

from gamelib.options import options

class DummyPlayer:
    def __getattr__(self, attr):
        return lambda x=None: None

def play(filename):
    if options.nomusic:
        return DummyPlayer()

    try:
        player = pyglet.media.Player()
    except pyglet.media.riff.WAVEFormatException:
        return DummyPlayer()

    player.queue(pyglet.resource.media(filename))
    player.eos_action = player.EOS_LOOP
    player.play()
    return player

